<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2017/4/4
 * Time: 15:45
 */

namespace app\mobile\controller;


class News extends Base
{
    public function index(){
        $slide = action('home/Poster/index',['id'=>5,'num'=>5,'city'=>$this->cityInfo['id']],'event');//切换广告
        $this->assign('news_baodao',$this->getListByCateId(2));
        $this->assign('news_bendi',$this->getListByCateId(3));
        $this->assign('news_loupan',$this->getListByCateId(4));
        $this->assign('news_goufang',$this->getListByCateId(9));
        $this->assign('slides',$slide);
        $this->assign('title','新闻资讯');
        $this->assign('time',time());
        return $this->fetch();
    }
    public function lists(){
        $keyword = input('get.keyword');
        $cate = cache('articleCate');
        $cate_id = input('param.cate_id/d',0);
        $where = 'status=1 and create_time < '.time();
        $info['name'] = '新闻资讯';
        if($cate_id){
            if(isset($cate['tree'][$cate_id]) && isset($cate['tree'][$cate_id]['_child'])){
                $child = $cate['tree'][$cate_id]['_child'];
                $child_id = array_keys($child);
                $where .= " and cate_id in (".implode(',',$child_id).")";

            }else{
                $where .= " and cate_id = ".$cate_id;
            }
            $cate['cate'][$cate_id]['pid'] && $cate_id = $cate['cate'][$cate_id]['pid'];
            $info['name'] = $cate['cate'][$cate_id]['name'];
        }
        if($keyword){
            $where .= " and title like '%".$keyword."%'";
            $this->assign('keyword',$keyword);
        }
        $this->setSeo($info,'name');
        $this->assign('cate_id',$cate_id);
        $this->assign('where',$where);
        $this->assign('title',$info['name']);
        return $this->fetch();
    }
    private function getListByCateId($cate_id,$num=5){
        $where = 'status = 1 and create_time < '.time();
        $cate = cache('articleCate');
        if(isset($cate['tree'][$cate_id]) && isset($cate['tree'][$cate_id]['_child'])){
            $child = $cate['tree'][$cate_id]['_child'];
            $child_id = array_keys($child);
            $where .= " and cate_id in (".implode(',',$child_id).")";
        }else{
            $where .= " and cate_id = ".$cate_id;
        }
        $lists = model('article')->where($where)->field('id,title,description')->order('ordid asc,id desc')->limit($num)->select();
        return $lists;
    }
    public function detail(){
        $id = input('param.id/d',0);
        $info='';$house_info='';
        if($id){
            $info = model('article')->where(['status'=>1,'id'=>$id])->find();
            if(!$info){
                return $this->fetch('public/404');
            }
            model('article')->where(['status'=>1,'id'=>$id])->setInc('hits');
            if($info['house_id']){
                $house_info = model('house')->field('id,title,img,city_name,area_name,average_price,address,sale_phone')->where(['id'=>$info['house_id'],'status'=>1])->find();
                $fuhao = empty($info['seo_keys'])?'':',';
                $info['seo_keys'] .= $fuhao.$house_info['title'];
            }
        }else{
            return $this->fetch('public/404');
        }
        $love_lists = model('article')->where(['cate_id'=>$info['cate_id'],'status'=>1,'create_time'=>['lt',time()]])->field('id,title,create_time')->order('id desc')->limit(8)->select();
        $this->setSeo($info);
        $this->assign('info',$info);
        $this->assign('house',$house_info);
        $this->assign('love_lists',$love_lists);
        return $this->fetch();
    }
}